package com.htht.smartschool.common.job;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.htht.smartschool.model.card.TCard;
import com.htht.smartschool.model.card.TFamilyphone;
import com.htht.smartschool.service.card.CardService;
import com.htht.smartschool.util.system.ConfigUtil;
import com.htht.smartschool.util.system.FtpUtil;

public class DownFileJob implements Job{
	static CardService cs = new CardService();
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		downloads();
		dealDownLoads();
	}
	
	public static void dealDownLoads() {
		File fp = new File(ConfigUtil.getProperty("ftp","downloads"));
		File ftemp = new File(ConfigUtil.getProperty("ftp","temp"));
		File[] fls = fp.listFiles();
		File[] tms = ftemp.listFiles();
		List<TFamilyphone> phones = new ArrayList<TFamilyphone>();
		for (int i = 0; i < fls.length; i++) {
			//处理后的文件
			File downfile = fls[i];
			File upfile = null;
			for (int j = 0; j < tms.length; j++) {
				if(downfile.getName().contains(tms[j].getName())){
					upfile = tms[j];
				}
			}
			String downresult = getFileString(downfile.getPath()).replaceAll("[\\t\\n\\r]", "\\|");
			String[] results = downresult.split("\\|");
			if(results.length>2){
				//设置失败
				//设置失败
				TFamilyphone phone = new TFamilyphone();
				List<String> list = Arrays.asList(results);
				list = list.subList(2, list.size());
				list = delNullString(list);
				for (int j = 0; j < list.size()/5; j++) {
					TCard tc = new TCard();
					tc.setCellphone(list.get(0));
					System.out.println(list.get(0));
					List<TCard> listtc = cs.getCard(tc);
					if(listtc.size()>0){
						TCard tcard = listtc.get(0);
						phone.setNumber(tcard.getImei());
					}
					phone.setNumber(list.get(0));
					phone.setFamilyphone(list.get(1));
					phone.setFlag("3");
					phone.setMsg(list.get(4));
					phones.add(phone);
				}
			}else{
				//设置成功
				List<String> sucresult = getFileList(upfile.getPath());
				for (String sucstr : sucresult) {
					String[] sucresults = sucstr.split("\\|");
					if(sucresults[2].equalsIgnoreCase("1")){
						TFamilyphone phone = new TFamilyphone();
						phone.setNumber(sucresults[0]);
						phone.setFamilyphone(sucresults[1]);
						phone.setFlag("1");
						phone.setMsg("处理成功");
						System.out.println(sucresults[1]);
						phones.add(phone);
					}
				}
			}
		}
		cs.updateFamilyphonesFlag(phones);
		for (int j = 0; j < tms.length; j++) {
			tms[j].delete();
		}
	}
	@SuppressWarnings("unchecked")
	public static List<String> getFileList(String filepath){
		List<String> result = new ArrayList<String>();
		try {
			DataInputStream read = new DataInputStream(new BufferedInputStream(new FileInputStream(filepath)));
			result= IOUtils.readLines(read);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	public static String getFileString(String filepath){
		String result = null;
		try {
			DataInputStream read = new DataInputStream(new BufferedInputStream(new FileInputStream(filepath)));
			result = IOUtils.toString(read, "gbk");
		} catch (IOException e) {
			e.printStackTrace();
		}
		result = result.replaceAll("[\\t\\n\\r]", "\\|");
		return result;
	}
	public static List<String> delNullString(List<String> list){
		List<String> result = new ArrayList<String>();
		for (String str : list) {
			if(str.length()>0){
				result.add(str);
			}
		}
		return result;
	}
	public static void main(String[] args) {
		downloads();
		dealDownLoads();
	}
	public static void downloads() {
		FtpUtil.downFile(ConfigUtil.getProperty("ftp","url"), Integer.parseInt(ConfigUtil.getProperty("ftp","port")),
				ConfigUtil.getProperty("ftp","username"), ConfigUtil.getProperty("ftp","password"), 
				ConfigUtil.getProperty("ftp","downpath"), ConfigUtil.getProperty("ftp","downloads"));
		FtpUtil.deleteFile(ConfigUtil.getProperty("ftp","url"), Integer.parseInt(ConfigUtil.getProperty("ftp","port")),
				ConfigUtil.getProperty("ftp","username"), ConfigUtil.getProperty("ftp","password"), 
				ConfigUtil.getProperty("ftp","downpath"));
	}
}
